package com.wangxiang.shop.product.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.wangxiang.common.utils.MyStringUtile;
import com.wangxiang.shop.product.dao.ClothingSpuDao;
import com.wangxiang.shop.product.dao.SkuDao;
import com.wangxiang.shop.product.dao.SpuDao;
import com.wangxiang.shop.product.dao.UniShopDao;
import com.wangxiang.shop.product.entity.ClothingSpuEntity;
import com.wangxiang.shop.product.entity.SkuEntity;
import com.wangxiang.shop.product.entity.SpuEntity;
import com.wangxiang.shop.product.entity.UniShopEntity;
import com.wangxiang.shop.product.service.UniShopService;
import com.wangxiang.shop.product.utils.SkuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class UniShopServiceImpl extends ServiceImpl<UniShopDao,UniShopEntity> implements UniShopService {
   @Autowired
   private SpuDao spuDao;
   @Autowired
   private SkuDao skuDao;
   @Autowired
   private SkuUtil skuUtil;

    /**
     * 前端获取首页导航安数据
     * @param type
     * @return
     */
    @Override
    public List<SpuEntity> getSpuInIndex(String type) {
        ArrayList<SpuEntity> list = new ArrayList<>();
        //首页
        if (type.equals("首页")){
            for (int i = 0; i < 100; i++) {
                int id = RandomUtil.randomInt(1, 162631);
                SpuEntity spuEntity = spuDao.selectById(id);
                SpuEntity spuEntity1 = new SpuEntity();
                if (spuEntity!=null){
                    spuEntity1.setId(spuEntity.getId());
                    spuEntity1.setStore(spuEntity.getStore());
                    spuEntity1.setImage(spuEntity.getImage());
                    spuEntity1.setPrice(spuEntity.getPrice());
                    spuEntity1.setTitle(spuEntity.getTitle());
                    list.add(spuEntity1);
                }

            }
        }



        return list;
    }

    @Override
    public SpuEntity getShop(int id) {
        SpuEntity spuEntity = spuDao.selectById(id);
        //sku数据
        if (spuEntity.getSkuId()!=null){
            MyStringUtile myStringUtile = new MyStringUtile();
            ArrayList<Integer> skuArray = myStringUtile.getSkuArray(spuEntity.getSkuId());
            ArrayList<SkuEntity> skuEntities = new ArrayList<>();
            for (Integer integer : skuArray) {
                SkuEntity skuEntity = skuDao.selectById(integer);
                skuEntities.add(skuEntity);
            }
            HashMap<String, List<String>> map = skuUtil.getStyleSizeImgList(skuEntities);
            spuEntity.setImgList(map.get("imgList"));
            spuEntity.setStyleList(map.get("styleList"));
            spuEntity.setSizeList(map.get("sizeList"));

        }
        return spuEntity;
    }
}
